﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TheatreManagementSystem
{
    public class Performance : IPrintable
    {
        private char[,] seatingChart;

        public DateTime DateOfPerformance { get; set; }
        public Play NameOfPlay { get; set; }
        public string HallName { get; set; }
        public decimal PriceOfPerformance { get; set; }

        public char[,] SeatingChart
        {
            get { return seatingChart; }
        }

        public Performance(Hall hall, DateTime dateAndTime, Play play, decimal price)
        {
            this.HallName = hall.Name;
            this.NameOfPlay = play;
            this.DateOfPerformance = dateAndTime;
            this.seatingChart = (char[,])hall.HallView.Clone();
            this.PriceOfPerformance = price;
        }

        public string PrepareForPrint()
        {
            int line = seatingChart.GetLength(1);
            if (line > this.NameOfPlay.ToString().Length)
            {
                int remainder = line - this.NameOfPlay.ToString().Length;
                if (remainder % 2 == 0)
                {
                    Console.WriteLine(new string(' ', remainder / 2) + this.NameOfPlay + new string(' ', remainder / 2));
                }
                else
                {
                    Console.WriteLine(new string(' ', remainder / 2) + this.NameOfPlay + new string(' ', remainder / 2 + 1));
                }

            }
            else
            {
                Console.WriteLine(this.NameOfPlay);
            }
            StringBuilder hall = new StringBuilder();
            
            for (int i = 0; i < this.seatingChart.GetLength(0); i++)
            {
                for (int j = 0; j < this.seatingChart.GetLength(1); j++)
                {
                    hall.Append(seatingChart[i, j]);
                }
                hall.AppendLine();
            }
            return hall.ToString();
        }

        public char isSeatTaken(Seat seat)
        {
            if (seat.Rows > seatingChart.GetLength(0) || seat.Cols > seatingChart.GetLength(1))
            {
                throw new SeatDoesNotExistException(seat);
            }
            else
            {
                return seatingChart[seat.Rows - 1, seat.Cols - 1];
            }

        }

        public void MakeSeatTaken(Seat seat)
        {
            if (seat.Rows > seatingChart.GetLength(0) || seat.Cols > seatingChart.GetLength(1))
            {
                throw new SeatDoesNotExistException(seat);
            }
            else
            {
                if (seat.Cols == ' ')
                {
                    throw new SeatDoesNotExistException(seat);
                }
                else
                {
                    this.seatingChart[seat.Rows - 1, seat.Cols - 1] = 'X';
                }
            }
        }
    }
}


//for (int i = 0; i < row + 4; i++)
//{
//    if (i==0)
//    {
//        Console.WriteLine(new string('*', col + 4));
//    }
//    else if (i==1)
//    {
//        Console.WriteLine(new string('*', 1) + new string(' ', col + 2) + new string('*', 1));
//    }
//    else if (i==row+2)
//    {
//        Console.WriteLine(new string('*', 1) + new string(' ', col + 2) + new string('*', 1));
//    }
//    else if (i==row+3)
//    {
//        Console.WriteLine(new string('*', col + 4));
//    }
//    else
//    {
//        Console.WriteLine(new string('*', 1)+" " + new string(' ', col )+ " "+ new string('*', 1));
//    }
//    //not very sure that we will use it this form

//}